Skip to content
This repository was archived by the owner on May 6, 2026. It is now read-only.

fix: prevent mismatch error with default namespace on ancestor queries#604

Closed
cguardia wants to merge 1 commit into
googleapis:masterfrom
cguardia:577-ancestor-queries
Closed

fix: prevent mismatch error with default namespace on ancestor queries#604
cguardia wants to merge 1 commit into
googleapis:masterfrom
cguardia:577-ancestor-queries

Conversation

@cguardia
Copy link
Copy Markdown
Contributor

fixes #577

@cguardia cguardia requested a review from andrewsg as a code owner February 13, 2021 09:47
@product-auto-label product-auto-label Bot added the api: datastore Issues related to the googleapis/python-ndb API. label Feb 13, 2021
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label Feb 13, 2021
entity3.put()
dispose_of(entity3.key._key)

with client_context.new(namespace=other_namespace).use():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, tests are not, by default, run in the default namespace, so you don't really have to switch namespace here. It's already not the default. Although, I guess it might make it more readable to show that the namespace is different explicitly.

Copy link
Copy Markdown
Contributor

@chrisrossi chrisrossi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my note about passing namespace to the model constructor.

parent_key = entity1.put()
dispose_of(entity1.key._key)

entity2 = Dummy(foo="child", parent=parent_key, namespace=None)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed in the customer's example that they don't explicitly pass namespace. I would think that the namespace would be implied by passing in parent. For grins, I tried removing the namespace arg from this test and I get a namespace mismatch exception in the model constructor. Is there maybe a little more work to do here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I confess I added this to make the test pass, but the customer's code does run unchanged if using a python script outside of the test machinery. Maybe I'm missing something on the test setup?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it's just because in the customer's example the context's namespace is still the default namespace when that entity is created, so there won't be a mismatch between entity2 and its parent. Does still seem like specifying parent should imply the namespace, though.

I know you're starting your new job. Would you like me to take this one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please take this. I appreciate the offer.

FWIW, the following code caused the mismatch error before the fix was applied, and it works fine with the fix in:

import os
from google.cloud import ndb

class Dummy(ndb.Model):
    foo = ndb.StringProperty(default='')

with ndb.Client().context():
    entity1 = Dummy(foo='bar', namespace='')
    entity1.put()
    key_entity1 = entity1.key

    entity2 = Dummy(foo='child', parent=key_entity1)
    entity2.put()

    entity3 = Dummy(foo='childless')
    entity3.put()

with ndb.Client().context(namespace='other_context'):
    l = Dummy.query(ancestor=key_entity1, namespace='').fetch()
    for e in l:
        print(e)

I couldn't quite duplicate that in the text.

@chrisrossi
Copy link
Copy Markdown
Contributor

Superceded by #614

@chrisrossi chrisrossi closed this Feb 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: datastore Issues related to the googleapis/python-ndb API. cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NDB: Cannot Query/fetch using ancestors stored in default namespace from another namespace

2 participants